package com.bajiao.service.order.impl;

import com.bajiao.entity.order.Order;
import com.bajiao.entity.order.OrderDetails;
import com.bajiao.mapper.order.OrderDetailsMapper;
import com.bajiao.service.order.OrderDetailsService;
import com.bajiao.vo.cart.CartResponse;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 订单详情表
 * @Author: FanJq
 * @Date:   2025-01-13
 * @Version: V1.0
 */
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsMapper, OrderDetails> implements OrderDetailsService {

    /**
     * @Description
     * 根据订单编号获取
     * @date 2025/1/13 18:45
     * @author FanJq
     * @param orderNo
     * @return {@link List< OrderDetails>}
    */
    @Override
    public List<OrderDetails> getByOrderNo(String orderNo) {
        List<OrderDetails> detailsList = this.lambdaQuery().eq(OrderDetails::getOrderNo, orderNo).list();
        return detailsList;
    }

    /**
     * @Description
     * 创建子订单
     * @date 2025/1/13 19:09
     * @author FanJq
     * @param order
     * @param cartList
     * @return {@link List< OrderDetails>}
    */
    @Override
    public List<OrderDetails> create(Order order, List<CartResponse> cartList) {
        List<OrderDetails> detailsList = cartList.stream().map(cart -> {
            OrderDetails details = new OrderDetails();
            details.setOrderNo(order.getOrderNo());
            details.setStoreId(order.getStoreId());
            details.setProductId(cart.getProductId());
            details.setProductTitle(cart.getTitle());
            details.setCover(cart.getCover());
            details.setPrice(cart.getPrice());
            details.setBuyNum(cart.getBuyNum());
            return details;
        }).collect(Collectors.toList());
        super.saveBatch(detailsList);
        return detailsList;
    }
}
